package com.frank.loadinglibrary.special.cool.letter;


import com.frank.loadinglibrary.utils.SimpleAnimatorListener;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;

/**
 * Created by yanxing on 16/2/19.
 */
public class GLetter extends BaseLetter {
    public final static int STROKE_WIDTH = 20;
    public final static int SHIFT = 60 - STROKE_WIDTH / 2;
    public final static int WIDTH = STROKE_WIDTH / 2 + 20;
    public final static int LEG_LENGTH = 120;
    private boolean isStart = false;
    private int mCurValue;
    private Path mPath;
    private Paint mPaint;
    // 圆
    private RectFloat mRectF;
    private int mStartAngle;
    private int mSweepAngle;
    // g的下半圆
    private RectFloat mRectFHalf;
    private int mSweepAngleHalf;
    private float mMoveX;
    private float mMoveY;
    private int mFv;
    // 判断是否进入了底部弧线绘制
    private boolean isInRoundDraw = false;

    public GLetter(float x, float y) {
        super(x, y);
        mPath = new Path();
        mMoveX = mCurX + SHIFT;
        mMoveY = mCurY - SHIFT;
        mPath.moveTo(mMoveX, mMoveY);
        mFv = mDuration * 2 / 3;
        mRectFHalf = new RectFloat();
        mRectFHalf.modify(mCurX - SHIFT, mCurY - SHIFT + LEG_LENGTH - SHIFT, mCurX + SHIFT, mCurY + SHIFT + LEG_LENGTH - SHIFT);
        mSweepAngleHalf = 0;

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setStrokeWidth(STROKE_WIDTH);
        mRectF = new RectFloat();
        mRectF.modify(mCurX - SHIFT, mCurY - SHIFT, mCurX + SHIFT, mCurY + SHIFT);
        mStartAngle = 0;
        mSweepAngle = 0;
    }

    @Override
    public void reset() {
        mMoveX = mCurX + SHIFT;
        mMoveY = mCurY - SHIFT;
        if (mPath != null) {
            mPath.reset();
            mPath.moveTo(mMoveX, mMoveY);
        }
        mFv = mDuration * 2 / 3;
        if (mRectFHalf != null) {
            mRectFHalf.modify(mCurX - SHIFT, mCurY - SHIFT + LEG_LENGTH - SHIFT, mCurX + SHIFT, mCurY + SHIFT + LEG_LENGTH - SHIFT);
        }
        mSweepAngleHalf = 0;

        if (mPaint != null) {
            mPaint.reset();
            mPaint.setAntiAlias(true);
            mPaint.setColor(Color.WHITE);
            mPaint.setStyle(Paint.Style.STROKE_STYLE);
            mPaint.setStrokeWidth(STROKE_WIDTH);

        }
        if (mRectF != null) {
            mRectF.modify(mCurX - SHIFT, mCurY - SHIFT, mCurX + SHIFT, mCurY + SHIFT);
        }
        mStartAngle = 0;
        mSweepAngle = 0;
        isStart = false;
        isInRoundDraw = false;
    }


    @Override
    public void startAnim() {
        AnimatorValue animator = new AnimatorValue();
        animator.setDuration(mDuration);
        animator.setValueUpdateListener((animatorValue, v) -> {
            if (!isStart) {
                return;
            }
            // 完整圆的路径
            mCurValue = (int) (mDuration * v);
            mSweepAngle = mCurValue * 360 / mDuration;
            // 计算g除了圆的J勾路线
            if (mCurValue < mFv) {
                mMoveY = mCurY - SHIFT + LEG_LENGTH * mCurValue / mFv;
                mPath.lineTo(mMoveX, mMoveY);
            } else {
                if (!isInRoundDraw) {
                    isInRoundDraw = true;
                    mMoveY = mCurY - SHIFT + LEG_LENGTH;
                    mPath.lineTo(mMoveX, mMoveY);
                }
                mCurValue -= mFv;
                mSweepAngleHalf = mCurValue * 180 / (mDuration - mFv);
                mPath.addArc(mRectFHalf, mStartAngle, mSweepAngleHalf);
            }
        });
        animator.setStateChangedListener(new SimpleAnimatorListener() {
            @Override
            public void onStart(Animator animator) {
                super.onStart(animator);
                isStart = true;
            }

            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                if (animationCallBack != null) {
                    animationCallBack.onLetterAnimatorEnd();
                }
            }
        });
        animator.start();
    }

    @Override
    public void drawSelf(Canvas canvas) {
        // 勾子
        canvas.drawPath(mPath, mPaint);
        // 圆
        canvas.drawArc(mRectF, new Arc(mStartAngle, mSweepAngle, false), mPaint);
    }
}
